#coding:utf-8
from django.shortcuts import render_to_response
from django.template.context import RequestContext
from django.contrib.auth.decorators import login_required
from lovewith.views import site_info
from lovewith.utils.mtcache import MtCache
from lovewith.share.views.modules.share import ShareApi
from lovewith.subscribe.views.modules.subscribe import SubscribeApi
from lovewith.subscribe.views.modules.recommand import get_recommand_keywords



#显示全部推荐订阅
@login_required
def show_all(request, page_no=1):
    if not page_no:
        page_no = 1
    page_no = int(page_no)

    command_keywords = get_recommand_keywords(page_no)
    user_sub_data = SubscribeApi.get_by_uid(request.user.id)

    return render_to_response('subscribe/all.jinja', {
        'current_page': page_no,
        'total_page': command_keywords.get('total_page'),
        'command_keywords': command_keywords.get('keywords'),
        'user_sub_data': user_sub_data,
    },context_instance = RequestContext(request, processors = [site_info]))



#获取用户订阅的内容
def get_subscribe_by_user(user, page_no):
    sub_data = MtCache('subscribe_share').get(user_id=user.id, page=page_no)

    if not sub_data:
        sub_data = {'total': 0, 'share': []}
        sub_keyword = SubscribeApi.get_by_uid(user.id)

        if len(sub_keyword) > 0:
            sub_data = ShareApi.get_by_user_subscribe(sub_keyword, page_no)
            MtCache('subscribe_share').set(sub_data, user_id=user.id, page=page_no)

    return sub_data